﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Physic = StillDesign.PhysX; //PhysX
using PhysicMath = StillDesign.PhysX.MathPrimitives; //PhysX
namespace Game
{
    /*-------------------------------------------------------------------------
    | PhysicTriggerEventSystem:
    | ------------
    | Implentation trigger event manager by physic
    |---------------------------------------------------------------------------*/

    public class PhysicSceneHitEventSystem : Physic.UserContactReport, ISceneHitEventSystem
    {
        private List<ISceneHitListener> mListeners = new List<ISceneHitListener>();

        public void Clear()
        {
            mListeners.Clear();
        }

        public void AddListener(ISceneHitListener hit)
        {
            mListeners.Add(hit);
        }

        public override void OnContactNotify(Physic.ContactPair contactInformation, Physic.ContactPairFlag events)
        {
            /*
            foreach (ContactReportPair pair in contactInformation)
            {

            }*/
            
            
            // contactInformation.NormalForce
            float force = contactInformation.NormalForce.Length();
            if (force > 0)
            {
               // contactInformation.
                foreach (var it in mListeners)
                {
                    it.OnHit(contactInformation.ActorA.Name, contactInformation.ActorB.Name, IDKToPhysic.ToVector3(contactInformation.NormalForce));
                }

               // Log.Warn(String.Format("{0}", force));

               // Log.Warn(contactInformation.ActorA.Name + "_" + contactInformation.ActorB.Name);
            }
        }
    }

    // MEM LEAK ON RUNTIME - CLEAN ONLY END APPLICATION
    class PhysicSceneTriggerEventSystem : Physic.UserTriggerReport, ISceneTriggerEventSystem
    {
        private List<ISceneTriggerListener> mListeners = new List<ISceneTriggerListener>();

        public void Clear()
        {
            mListeners.Clear();
        }

        public void AddListener(ISceneTriggerListener trigger)
        {
            mListeners.Add(trigger);
        }

        public override void OnTrigger(Physic.Shape triggerShape, Physic.Shape otherShape, Physic.TriggerFlag status)
        {
            switch (status)
            {
                case Physic.TriggerFlag.OnEnter:
                    foreach (var it in mListeners)
                    {
                        it.OnEnter(triggerShape.Actor.Name, otherShape.Actor.Name);
                    }
                    break;

                case Physic.TriggerFlag.OnLeave:
                    foreach (var it in mListeners)
                    {
                        it.OnLeave(triggerShape.Actor.Name, otherShape.Actor.Name);
                    }
                    break;

                case Physic.TriggerFlag.OnStay:
                    foreach (var it in mListeners)
                    {
                        it.OnStay(triggerShape.Actor.Name, otherShape.Actor.Name);
                    }
                    break;
            }
        }
    }
}
